﻿// Fill out your copyright notice in the Description page of Project Settings.


#include "ResourceOperation/SynchronousLoading.h"
#include "Engine/AssetManager.h"

void USynchronousLoading::LoadObjectMethod()
{
	UMaterial* M_Cube = LoadObject<UMaterial>(nullptr, TEXT("Material'/Game/Geometry/Meshes/CubeMaterial.CubeMaterial'"));
	if (M_Cube)
	{
		// UE_LOG(LogTemp, Warning, TEXT("Material name:%s"), *M_Cube->GetName());
	}
}

void USynchronousLoading::LoadClassMethod()
{
	UClass* pClass = LoadClass<AActor>(nullptr, TEXT("Blueprint'/Game/CPPFunction/Load/BP_LoadActor.BP_LoadActor_C'"));
	if (pClass)
	{
		// UE_LOG(LogTemp, Warning, TEXT("pClass name:%s"), *pClass->GetName());
	}
	TSubclassOf<AActor> BPClass = LoadClass<AActor>(nullptr, TEXT("Blueprint'/Game/CPPFunction/Load/BP_MyActor'"));
	if (BPClass)
	{
		// UE_LOG(LogTemp, Warning, TEXT("BPClass name:%s"), *BPClass->GetName());
	}
}

void USynchronousLoading::TryLoadMethod()
{
	FSoftObjectPath SoftObjectPaths_Mesh = FSoftObjectPath(TEXT("StaticMesh'/Game/CPPFunction/Load/StaticMesh.StaticMesh'"));
	UStaticMesh* Mesh1 = Cast<UStaticMesh>(SoftObjectPaths_Mesh.TryLoad());
	if (Mesh1)
	{
		// UE_LOG(LogTemp, Warning, TEXT("Mesh1 name:%s"), *Mesh1->GetName());
	}
}

void USynchronousLoading::TryLoadClassMethod()
{
	FSoftClassPath SoftClassPath_Actor = FSoftClassPath(TEXT("Blueprint'/Game/CPPFunction/Load/BP_MyActor_SoftRef.BP_MyActor_SoftRef_C'"));
	UClass* pClass_Actor = SoftClassPath_Actor.TryLoadClass<AActor>();
	if (pClass_Actor)
	{
		// UE_LOG(LogTemp, Warning, TEXT("pClass_Actor name:%s"), *pClass_Actor->GetName());
	}
}

void USynchronousLoading::LoadSynchronousMethod()
{
	FStreamableManager& streamableManager = UAssetManager::GetStreamableManager();
	// 可加载非蓝图资源类
	// 配合 FSoftObjectPath 使用 方法一
	FSoftObjectPath SoftObjectPaths_Mesh1 = FSoftObjectPath(TEXT("StaticMesh'/Game/CPPFunction/Load/StaticMesh.StaticMesh'"));
	UStaticMesh* StaticMesh1 = Cast<UStaticMesh>(streamableManager.LoadSynchronous(SoftObjectPaths_Mesh1));
	if (StaticMesh1)
	{
		// UE_LOG(LogTemp, Warning, TEXT("Mesh1 name:%s"), *StaticMesh1->GetName());
	}
	// 配合 FSoftObjectPath 使用 方法二
	FSoftObjectPath SoftObjectPaths_Mesh2 = FSoftObjectPath(TEXT("StaticMesh'/Game/CPPFunction/Load/StaticMesh_Soft.StaticMesh_Soft'"));
	UStaticMesh* StaticMesh2 = streamableManager.LoadSynchronous<UStaticMesh>(SoftObjectPaths_Mesh2);
	if (StaticMesh2)
	{
		// UE_LOG(LogTemp, Warning, TEXT("Mesh2 name:%s"), *StaticMesh2->GetName());
	}
	// 配合 TSoftObjectPtr<T> 使用
	FSoftObjectPath SoftObjectPaths_Mesh3 = FSoftObjectPath(TEXT("StaticMesh'/Game/CPPFunction/Load/StaticMesh_Soft2.StaticMesh_Soft2'"));
	TSoftObjectPtr<UStaticMesh> SoftObjectPtr_Mesh = TSoftObjectPtr<UStaticMesh>(SoftObjectPaths_Mesh3);
	UStaticMesh* StaticMesh3 = streamableManager.LoadSynchronous(SoftObjectPtr_Mesh); //保持良好习惯<UStaticMesh>
	if (StaticMesh3)
	{
		// UE_LOG(LogTemp, Warning, TEXT("Mesh3 name:%s"), *StaticMesh3->GetName());
	}

	// 也可加载蓝图类为 UClass*
	FSoftObjectPath SoftObjectPaths_Actor1 = FSoftObjectPath(TEXT("Blueprint'/Game/CPPFunction/Load/BP_MyActor.BP_MyActor_C'"));
	UClass* BPClass1 = streamableManager.LoadSynchronous<UClass>(SoftObjectPaths_Actor1);
	if (BPClass1)
	{
		// UE_LOG(LogTemp, Warning, TEXT("BPClass1 name:%s"), *BPClass1->GetName());
	}
	// 配合 FSoftObjectPath 使用 方法二
	FSoftObjectPath SoftObjectPaths_Actor2 = FSoftObjectPath(TEXT("Blueprint'/Game/CPPFunction/Load/BP_MyActor_SoftRef.BP_MyActor_SoftRef_C'"));
	UClass* BPClass2 = streamableManager.LoadSynchronous<UClass>(SoftObjectPaths_Actor2);
	if (BPClass2)
	{
		// UE_LOG(LogTemp, Warning, TEXT("BPClass2 name:%s"), *BPClass2->GetName());
	}
	// 配合 TSoftObjectPtr<T> 使用
	FSoftObjectPath SoftObjectPaths_Actor3 = FSoftObjectPath(TEXT("Blueprint'/Game/CPPFunction/Load/BP_MyActor_SoftRef2.BP_MyActor_SoftRef2_C'"));
	TSoftObjectPtr<UClass> SoftObjectPtr_Actor = TSoftObjectPtr<UClass>(SoftObjectPaths_Actor3);
	UClass* BPClass3 = streamableManager.LoadSynchronous(SoftObjectPtr_Actor); //保持良好习惯可添加<UClass>
	if (BPClass3)
	{
		// UE_LOG(LogTemp, Warning, TEXT("BPClass3 name:%s"), *BPClass3->GetName());
	}
}

void USynchronousLoading::RequestSyncLoadMethod()
{
	// 获取单个资源 方法一
	FSoftObjectPath SoftObjectPaths_Mesh1 = FSoftObjectPath(TEXT("StaticMesh'/Game/CPPFunction/Load/StaticMesh.StaticMesh'"));
	TSharedPtr<FStreamableHandle> Handle1 = UAssetManager::GetStreamableManager().RequestSyncLoad(SoftObjectPaths_Mesh1);
	if (Handle1.IsValid())
	{
		UStaticMesh* StaticMesh1 = Cast<UStaticMesh>(Handle1->GetLoadedAsset());
		// UE_LOG(LogTemp, Warning, TEXT("Mesh1 name:%s"), *StaticMesh1->GetName());
	}
	// 获取单个资源 方法二
	FStreamableManager& streamableManager = UAssetManager::GetStreamableManager();
	FSoftObjectPath SoftObjectPaths_Mesh2 = FSoftObjectPath(TEXT("StaticMesh'/Game/CPPFunction/Load/StaticMesh_Soft.StaticMesh_Soft'"));
	TSharedPtr<FStreamableHandle> Handle2 = streamableManager.RequestSyncLoad(SoftObjectPaths_Mesh2);
	if (Handle2.IsValid())
	{
		UStaticMesh* StaticMesh2 = Cast<UStaticMesh>(Handle2->GetLoadedAsset());
		// UE_LOG(LogTemp, Warning, TEXT("Mesh1 name:%s"), *StaticMesh2->GetName());
	}
	// 获取多个资源
	TArray<FSoftObjectPath> SoftObjectPaths;
	SoftObjectPaths.Add(SoftObjectPaths_Mesh1);
	SoftObjectPaths.Add(SoftObjectPaths_Mesh2);
	TSharedPtr<FStreamableHandle> Handle3 = streamableManager.RequestSyncLoad(SoftObjectPaths);
	{
		TArray<UObject*> Objects;
		Handle3->GetLoadedAssets(Objects);
		for (UObject* item : Objects)
		{
			UStaticMesh* StaticMesh3 = Cast<UStaticMesh>(item);
			// UE_LOG(LogTemp, Warning, TEXT("GetLoadedAssets(), item name:%s"), *StaticMesh3->GetName());
		}
	}
}
